//*********************************************************************
//		Copyright (c) 2003 - 2008 Microsoft Corporation.
//      This code is provided under Microsoft Public License, which can be found at
//      http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx 
//
// @File: MainForm.cs
//
// Purpose: Main Form
//
//
//
// Notes: 
//
//
//	
// History: 
//     @Version: V2.1
//
//     08/18/08 PGV 2.1.001.0 Starting V2.1 (based on 2.0.030.0 code)
//
// @EndHeader@
//*********************************************************************

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.IO;
using System.Diagnostics;

namespace H2V2.ConfigUtility
{
	public struct st_repository 
	{
		public string Server;
		public string Database;
		public bool bIntSec;
		public string User;
		public string Pwd;
	}

	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem mnRepository;
		private System.Windows.Forms.MenuItem mnRepositoryInstall;
		private System.Windows.Forms.MenuItem mnRepositoryCheck;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public static Configurator Cfg;
		public static st_repository stRep;
		public static string curConfigFile = "H2Config.xml";

		// for Wizard usage only
		int m_Run = 0;
		public static bool WizardMode = false;
		public static DbInstaller Dbi;
		public static bool bWatson = false;
		public static string BinPath = "";

		private System.Windows.Forms.MenuItem mnConfig;
		private System.Windows.Forms.MenuItem mnConfigEdit;
		private System.Windows.Forms.MenuItem mnHelp;
		private System.Windows.Forms.MenuItem mnHelpAbout;
		private System.Windows.Forms.MenuItem mnWizards;
		private System.Windows.Forms.MenuItem mnICWizard;

		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			Cfg = new Configurator ();
			stRep.Server = "(local)";
			stRep.Database = "SQLH2Repository";
			stRep.bIntSec = true;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.mnRepository = new System.Windows.Forms.MenuItem();
			this.mnRepositoryInstall = new System.Windows.Forms.MenuItem();
			this.mnRepositoryCheck = new System.Windows.Forms.MenuItem();
			this.mnConfig = new System.Windows.Forms.MenuItem();
			this.mnConfigEdit = new System.Windows.Forms.MenuItem();
			this.mnWizards = new System.Windows.Forms.MenuItem();
			this.mnICWizard = new System.Windows.Forms.MenuItem();
			this.mnHelp = new System.Windows.Forms.MenuItem();
			this.mnHelpAbout = new System.Windows.Forms.MenuItem();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mnRepository,
																					  this.mnConfig,
																					  this.mnWizards,
																					  this.mnHelp});
			// 
			// mnRepository
			// 
			this.mnRepository.Index = 0;
			this.mnRepository.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.mnRepositoryInstall,
																						 this.mnRepositoryCheck});
			this.mnRepository.Text = "Repository";
			// 
			// mnRepositoryInstall
			// 
			this.mnRepositoryInstall.Index = 0;
			this.mnRepositoryInstall.Text = "Install";
			this.mnRepositoryInstall.Click += new System.EventHandler(this.mnRepositoryInstall_Click);
			// 
			// mnRepositoryCheck
			// 
			this.mnRepositoryCheck.Index = 1;
			this.mnRepositoryCheck.Text = "View";
			this.mnRepositoryCheck.Click += new System.EventHandler(this.mnRepositoryCheck_Click);
			// 
			// mnConfig
			// 
			this.mnConfig.Index = 1;
			this.mnConfig.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.mnConfigEdit});
			this.mnConfig.Text = "Config file";
			// 
			// mnConfigEdit
			// 
			this.mnConfigEdit.Index = 0;
			this.mnConfigEdit.Text = "Edit";
			this.mnConfigEdit.Click += new System.EventHandler(this.mnConfigEdit_Click);
			// 
			// mnWizards
			// 
			this.mnWizards.Index = 2;
			this.mnWizards.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mnICWizard});
			this.mnWizards.Text = "Wizard";
			// 
			// mnICWizard
			// 
			this.mnICWizard.Index = 0;
			this.mnICWizard.Text = "Installation Wizard";
			this.mnICWizard.Click += new System.EventHandler(this.mnICWizard_Click);
			// 
			// mnHelp
			// 
			this.mnHelp.Index = 3;
			this.mnHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																				   this.mnHelpAbout});
			this.mnHelp.Text = "Help";
			// 
			// mnHelpAbout
			// 
			this.mnHelpAbout.Index = 0;
			this.mnHelpAbout.Text = "About";
			this.mnHelpAbout.Click += new System.EventHandler(this.mnHelpAbout_Click);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(440, 318);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.Menu = this.mainMenu1;
			this.Name = "MainForm";
			this.Text = "SQLH2 Configuration Utility";
			this.Activated += new System.EventHandler(this.ICWizardWrapper);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main (string[] args) 
		{
			if (args.Length == 2 && args[0] == "/i")
			{
				WizardMode = true;
				BinPath = args[1];  
				if (!Directory.Exists (BinPath))
				{
					MessageBox.Show ("Supplied path \""+BinPath+"\" doesn't exist");
					return;
				}
			}

			Application.Run(new MainForm());
		}

		private void mnExit_Click(object sender, System.EventArgs e)
		{
			Application.Exit ();
		}

		private void mnRepositoryInstall_Click(object sender, System.EventArgs e)
		{
			Install ();
		}

		private void mnConfigEdit_Click(object sender, System.EventArgs e)
		{
			Configure ();
		}

		private void mnRepositoryCheck_Click(object sender, System.EventArgs e)
		{
			RepConnect frc = new RepConnect ();
			DialogResult dr = frc.ShowDialog ();
		}

		private void mnHelpAbout_Click(object sender, System.EventArgs e)
		{
			AboutForm fa = new AboutForm ();
			fa.ShowDialog ();
		}

		void Install ()
		{
			InstallForm finst = new InstallForm ();
			DialogResult dr = finst.ShowDialog ();
		}

		void Configure ()
		{
			CfgForm fcfg = new CfgForm ();
			DialogResult dr = fcfg.ShowDialog ();
		}

		void ICWizardWrapper (object sender, System.EventArgs e)
		{
			if (WizardMode && m_Run == 0)
			{
				m_Run++;

				Thread RunThread = new Thread (new ThreadStart (this.RunICWizard));
				RunThread.Start ();
			}
		}

		void RunICWizard ()
		{
			DialogResult dr;

			FormW1 fWelcome = new FormW1 ();
			dr = fWelcome.ShowDialog (this);
			if (dr != DialogResult.OK) goto EXIT_WIZARD;

			if (!CheckBCP ())
			{
				MessageBox.Show ("Cannot find bcp.exe\n"
					+"To perfrom export/import operations SQLH2 requires bcp.exe to be on the box.\n"
					+"You can install BCP utility by installing SQL Server Client tools on the system"
					, "Checking dependencies",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}

			DB_STEP:
			FormW2 fRConnect = new FormW2 ();
			dr = fRConnect.ShowDialog (this);
			if (dr != DialogResult.OK) goto EXIT_WIZARD;

            FormW3 fRCreate = new FormW3 ();
            dr = fRCreate.ShowDialog (this);
            if (dr != DialogResult.OK) goto DB_STEP;

			FormW4 fConfig = new FormW4 ();
			dr = fConfig.ShowDialog (this);
			if (dr != DialogResult.OK) goto EXIT_WIZARD;
		
			FormW5 fTask = new FormW5 ();
			dr = fTask.ShowDialog (this);
			if (dr != DialogResult.OK) goto EXIT_WIZARD;
		
			FormW6 fEnd = new FormW6 ();
			dr = fEnd.ShowDialog (this);

			EXIT_WIZARD:
			if (WizardMode) 
			{
				Close ();
			}

		}

		bool CheckBCP ()
		{
			bool ret = true;
			Process p=new Process ();
			p.StartInfo.FileName = "bcp.exe";
			p.StartInfo.CreateNoWindow = true;
			p.StartInfo.UseShellExecute = false;

			try
			{
				p.Start ();
			}
			catch (Exception)
			{
				ret = false;
			}
			finally
			{
				p.Close ();
			}

			return ret;
		}

		private void mnICWizard_Click(object sender, System.EventArgs e)
		{
			RunICWizard ();
		}

		public static bool ValidateName (string name)
		{
			bool ret = false;
			
			if (name != null)
			{
				if (name.Length > 0 && name.Length < 256 
					&& name.IndexOfAny ("~!@#$^&*()=+[]{}\\|;:'\",<>/? ".ToCharArray ()) == -1
					)
					{
						ret = true;
					}
			}

			return ret;
		}


	}
}
